export default {
  methods: {
    appendTableRow(widget) {
      this.designer.appendTableRow(widget)
    },

    appendTableCol(widget) {
      this.designer.appendTableCol(widget)
    },

    onContainerDragAdd(evt, subList) {
      const newIndex = evt.newIndex
      if (!!subList[newIndex]) {
        this.designer.setSelected( subList[newIndex] )
      }

      this.designer.emitHistoryChange()
      this.designer.emitEvent('field-selected', this.widget)
    },

    onContainerDragUpdate() {
      this.designer.emitHistoryChange()
    },

    checkContainerMove(evt) {
      return this.designer.checkWidgetMove(evt)
    },

    selectWidget(widget) {
      this.designer.setSelected(widget)
    },

    selectParentWidget() {
      if (this.parentWidget) {
        this.designer.setSelected(this.parentWidget)
      } else {
        this.designer.clearSelected()
      }
    },

    moveUpWidget() {
      this.designer.moveUpWidget(this.parentList, this.indexOfParentList)
      this.designer.emitHistoryChange()
    },

    moveDownWidget() {
      this.designer.moveDownWidget(this.parentList, this.indexOfParentList)
      this.designer.emitHistoryChange()
    },

    cloneContainer(widget) {
      if (!!this.parentList) {
        let newCon = this.designer.cloneContainer(widget)
        this.parentList.splice(this.indexOfParentList + 1, 0, newCon)
        this.designer.setSelected(newCon)

        this.designer.emitHistoryChange()
      }
    },

    removeWidget() {
      let sW = this.designer.selectedWidget;
      let tof = false;

      if (sW.type == 'table'){
        sW.rows.forEach(row=>{
          row.cols.forEach(col=>{
            col.widgetList.forEach(one=>{
              if(one.type == 'input' && one.options.is_fixed == 1){
                tof = true;
              }
            })
          })
        })
      }else if(sW.type == 'card'){
        sW.widgetList.forEach(wL=>{
          if (wL.type == 'grid'){//卡放栅格
            wL.cols.forEach(col=>{
              let wLl = col.widgetList;
              wLl.forEach(one=>{
                if(one.type == 'input' && one.options.is_fixed == 1){
                  tof = true;
                }
              })
            })
          }else if (wL.type == 'table'){//卡放表格
            wL.rows.forEach(row=>{
              row.cols.forEach(col=>{
                col.widgetList.forEach(one=>{
                  if(one.type == 'input' && one.options.is_fixed == 1){
                    tof = true;
                  }
                })
              })
            })
          }else if(wL.type == 'tab'){
            wL.tabs.forEach(tab=>{
              tab.widgetList.forEach(wLt=>{
                if (wLt.type == 'grid'){//放栅格
                  wLt.cols.forEach(col=>{
                    let wLl = col.widgetList;
                    wLl.forEach(one=>{
                      if(one.type == 'input' && one.options.is_fixed == 1){
                        tof = true;
                      }
                    })
                  })
                }else if (wLt.type == 'table'){//放表格
                  wLt.rows.forEach(row=>{
                    row.cols.forEach(col=>{
                      col.widgetList.forEach(one=>{
                        if(one.type == 'input' && one.options.is_fixed == 1){
                          tof = true;
                        }
                      })
                    })
                  })
                }else if(wLt.type == 'input' && wLt.options.is_fixed == 1){
                  tof = true;
                }
              })
            })
          }else if(wL.type == 'input' && wL.options.is_fixed == 1){
            tof = true;
          }
        })
      }else if(sW.type == 'grid'){
        sW.cols.forEach(col=>{
          let wL = col.widgetList;
          wL.forEach(one=>{
            if(one.type == 'input' && one.options.is_fixed == 1){
              tof = true;
            }
          })
        })
      }else if(sW.type == 'tab'){
        sW.tabs.forEach(tab=>{
          tab.widgetList.forEach(wL=>{ 
            if(wL.type == 'card'){//tab放卡片
              wL.widgetList.forEach(wLc=>{
                if (wLc.type == 'grid'){//放栅格
                  wLc.cols.forEach(col=>{
                    let wLl = col.widgetList;
                    wLl.forEach(one=>{
                      if(one.type == 'input' && one.options.is_fixed == 1){
                        tof = true;
                      }
                    })
                  })
                }else if (wLc.type == 'table'){//放表格
                  wLc.rows.forEach(row=>{
                    row.cols.forEach(col=>{
                      col.widgetList.forEach(one=>{
                        if(one.type == 'input' && one.options.is_fixed == 1){
                          tof = true;
                        }
                      })
                    })
                  })
                }else if(wLc.type == 'input' && wLc.options.is_fixed == 1){
                  tof = true;
                }
              })
            }else if (wL.type == 'grid'){//tab放栅格
              wL.cols.forEach(col=>{
                let wLl = col.widgetList;
                wLl.forEach(one=>{
                  if(one.type == 'input' && one.options.is_fixed == 1){
                    tof = true;
                  }
                })
              })
            }else if (wL.type == 'table'){//tab放表格
              wL.rows.forEach(row=>{
                row.cols.forEach(col=>{
                  col.widgetList.forEach(one=>{
                    if(one.type == 'input' && one.options.is_fixed == 1){
                      tof = true;
                    }
                  })
                })
              })
            }else if(wL.type == 'input' && wL.options.is_fixed == 1){
              tof = true;
            }
          })
        })
      }
      
      if(tof){
        this.$message({message:'子组件包含固定字段，禁止删除！',type:'error'});
      }else{
        if (!!this.parentList) {
          let nextSelected = null
          if (this.parentList.length === 1) {
            if (!!this.parentWidget) {
              nextSelected = this.parentWidget
            }
          } else if (this.parentList.length === (1 + this.indexOfParentList)) {
            nextSelected = this.parentList[this.indexOfParentList - 1]
          } else {
            nextSelected = this.parentList[this.indexOfParentList + 1]
          }

          this.$nextTick(() => {
            this.parentList.splice(this.indexOfParentList, 1)
            //if (!!nextSelected) {
            this.designer.setSelected(nextSelected)
            //}

            this.designer.emitHistoryChange()
          })
        }
      }
    },

    setWidgetOption(optionName, optionValue) { //通用组件选项修改API
      if (this.widget.options.hasOwnProperty(optionName)) {
        this.widget.options[optionName] = optionValue
      }
    },

  }
}
